package com.sgb.kjwl.viewmodel.self.packers;

import android.util.Log;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.ResponseList;
import com.sgb.kjwl.model.entity.self.SelfTeamAlbumEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.model.entity.self.SelfWorkerPublishEntity;
import com.sgb.kjwl.model.entity.self.TeamUploadAlbumEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.fragment.self.FragAlbumFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FragAlbumModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private FragAlbumFragment fragment;

    private int page = 1;
    private int size = 20;

    public FragAlbumModel(FragAlbumFragment fragment, SelfRepertory selfRepertory) {
        this.fragment = fragment;
    }

    public void requestTeamAlbumData(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("page", page);
        map.get().put("limit", size);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.get().put("companyNo", "");
            map.get().put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.get().put("companyNo", companyNo);
            map.get().put("userNo", "");
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().requestTeamAlbum(map.get()).enqueue(new Callback<BaseEntity<ResponseList<List<SelfTeamAlbumEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ResponseList<List<SelfTeamAlbumEntity>>>> call, Response<BaseEntity<ResponseList<List<SelfTeamAlbumEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getPhotoData(response.body().getData(), isLoadMore);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<SelfTeamAlbumEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //保存到服务器
    public void savePhotoData(TeamUploadAlbumEntity list) {

        DialogHelper.showProgressDialog(fragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().addTeamAlbum(list).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    EventBus.getDefault().post("save_photo_data");
                }
                MToast.showToast(fragment.getContext(), response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void uploadMutiFileModel(File file, int completeNum, int total) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "图片上传中" + completeNum + "/" + total + "...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.uploadImgSuccess(response.body().getData().toString());
                    Log.v("uploadFile", "success--single");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getContext(), t.getMessage());
            }
        });
    }

    // -------------------------------------------工队简介-------------------------------------------

    public void getTeamIntroduce() {
        Map map = new HashMap();
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getTeamIntroduce(map).enqueue(new Callback<BaseEntity<SelfTeamIntroduceEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SelfTeamIntroduceEntity>> call, Response<BaseEntity<SelfTeamIntroduceEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    fragment.updateIntroduce(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SelfTeamIntroduceEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
